import java.io.File;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;

public class FileEncAndDec {
    private static final int numOfEncAndDec = 0x99;    //加密解密秘钥
    public static void main(String[] args) {
        File srcFile = new File("D:\\jar.zip");    //初始文件
        File encFile = new File("D:\\enp.zip");    //加密文件
        File decFile = new File("D:\\decEnpFile.zip"); //解密文件
        try {
            //EncFile(srcFile, encFile);    //加密操作
            DecFile(encFile,decFile);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    private static void EncFile(File srcFile, File encFile) throws Exception {
        if (!srcFile.exists()) {
            System.out.println("source file   not    exixt");
            return;
        }
        if (!encFile.exists()) {
            System.out.println("encrypt    file   created");
            encFile.createNewFile();
        }
        InputStream fis = new FileInputStream(srcFile);
        OutputStream fos = new FileOutputStream(encFile);
        int n  = -1;    //文件字节内容
        //一次性取多少字节
        byte[] bytes = new byte[2048];
        while ((n = fis.read(bytes,0,bytes.length)) > -1) {
            bytes[0] ^=numOfEncAndDec;
            fos.write(bytes,0,n);
        }
        fis.close();
        fos.flush();
        fos.close();
    }
    private static void DecFile(File encFile, File decFile) throws Exception {
        if (!encFile.exists()) {
            System.out.println("encrypt file not exixt");
            return;
        }
        if (!decFile.exists()) {
            System.out.println("decrypt file created");
            decFile.createNewFile();
        }
        InputStream fis = new FileInputStream(encFile);
        OutputStream fos = new FileOutputStream(decFile);
        int n  = -1;    //文件字节内容
        //一次性取多少字节
        byte[] bytes = new byte[2048];
        while ((n = fis.read(bytes,0,bytes.length)) > -1) {
            bytes[0] ^=numOfEncAndDec;
            fos.write(bytes,0,n);
        }
        fis.close();
        fos.flush();
        fos.close();
    }
}